<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px" size="small">
      <el-form-item label="采集设备名称" prop="equiName">
        <el-input v-model="dataForm.equiName" placeholder="采集设备名称" maxlength="100"></el-input>
      </el-form-item>
      <el-form-item label="采集设备IP" prop="equiIp">
        <el-input v-model="dataForm.equiIp" placeholder="采集设备IP" maxlength="100"></el-input>
      </el-form-item>
      <el-form-item label="采集开启端口" prop="equiPort">
        <el-input v-model="dataForm.equiPort" placeholder="采集设备端口" maxlength="10"></el-input>
      </el-form-item>
      <el-form-item label="采集设备安装地址">
        <el-input v-model="dataForm.equiAddress" placeholder="采集设备安装地址" maxlength="100"></el-input>
      </el-form-item>
      <el-form-item label="设备序列号">
        <el-input v-model="dataForm.sn" placeholder="设备序列号" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="区域编号">
        <el-input v-model="dataForm.buildingId" placeholder="区域编号" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="网关号">
        <el-input v-model="dataForm.gatewayId" placeholder="网关号" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="数据版本号">
        <el-input v-model="dataForm.configVer" placeholder="数据版本号" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="数据内容">
        <el-input v-model="dataForm.configContent" placeholder="数据内容"  type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false" size="small">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" size="small">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {isIp} from '@/utils/validate'
  export default {
    data () {
      var validateIp = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('采集设备IP不能为空'));
        } else {
          if (!isIp(value)) {
            callback(new Error('采集设备IP格式不对'));
          } else {
            callback();
          }
        }
      };
      return {
        visible: false,
        dataForm: {
          equiCode: 0,
          equiName: '',
          equiIp: '',
          equiPort: '',
          equiAddress: '',
          sn: '',
          equiLastupdate: '',
          buildingId:'',
          gatewayId:'',
          configVer:'',
          configContent:''
        },
        dataRule: {
          equiName: [
            { required: true, message: '采集设备名称不能为空', trigger: 'blur' }
          ],
          equiIp: [
            { required: true, message: '采集设备不能为空', trigger: 'blur' },
            { validator: validateIp, trigger: 'blur' }
          ],
          equiPort: [
            { required: true, message: '采集设备端口不能为空', trigger: 'blur' }
          ],
          equiAddress: [
            { required: true, message: '采集设备安装地址不能为空', trigger: 'blur' }
          ],
          sn: [
            { required: true, message: '设备序列号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.equiCode = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.equiCode) {
            this.$api.okhttp({
              url: `/ecs/ecscollectequi/info/${this.dataForm.equiCode}`,
              method: 'get',
              params: {}
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.equiName = data.ecscollectequi.equiName
                this.dataForm.equiIp = data.ecscollectequi.equiIp
                this.dataForm.equiPort = data.ecscollectequi.equiPort
                this.dataForm.equiAddress = data.ecscollectequi.equiAddress
                this.dataForm.sn = data.ecscollectequi.sn
                this.dataForm.equiLastupdate = data.ecscollectequi.equiLastupdate
                this.dataForm.buildingId = data.ecscollectequi.buildingId
                this.dataForm.gatewayId = data.ecscollectequi.gatewayId
                this.dataForm.configVer = data.ecscollectequi.configVer
                this.dataForm.configContent = data.ecscollectequi.configContent
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.okhttp({
              url: `/ecs/ecscollectequi/${!this.dataForm.equiCode ? 'save' : 'update'}`,
              method: 'post',
              data: {
                'equiCode': this.dataForm.equiCode || undefined,
                'equiName': this.dataForm.equiName,
                'equiIp': this.dataForm.equiIp,
                'equiPort': this.dataForm.equiPort,
                'equiAddress': this.dataForm.equiAddress,
                'sn': this.dataForm.sn,
                "customerId": this.$util.cookies.get('customer_id'),
                "buildingId":this.dataForm.buildingId,
                "gatewayId":this.dataForm.gatewayId,
                "configVer":this.dataForm.configVer,
                "configContent": this.dataForm.configContent
              }
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
